package com.kl.controller;


//import com.kl.mapper.UserMapper;
//import com.kl.service.IUserService;

import com.alibaba.fastjson.JSONObject;
import com.kl.domain.UserDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.User;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.*;

import java.security.KeyPair;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * Users and global privileges 前端控制器
 * </p>
 *
 * @author 明敬涵
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "登录方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", required = true,
                    paramType = "query", dataTypeClass = User.class),
            @ApiImplicitParam(name = "password", value = "密码", required = true,
                    paramType = "query", dataTypeClass = User.class)
    })
    @PostMapping
    public Result login(@RequestParam("name") String name, @RequestParam("password") String password){
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(name, password);
        Map<String, Object> map = new HashMap<>();
        try {
            subject.login(token);

        } catch (UnknownAccountException e) {
            System.out.println("账号不存在");
            return Result.build(ConstantEnum.UNREGISTERED);
        } catch (IncorrectCredentialsException e) {
            System.out.println("密码错误");
            return Result.build(ConstantEnum.UNREGISTERED);
        } catch (Exception e) {
            System.out.println("账号被冻结");
            map.put("message", e.getMessage());
            return Result.build(ConstantEnum.ACCOUNT_IS_FROZEN);
        }

        User user = (User) subject.getPrincipal();
        //        Result result = userService.login(name,password,request);

        //随机生成的key
        String key = UUID.randomUUID().toString();


        // 缓存至redis
        //renewToken(jwtToken, user.getId());
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user , userDTO);
        String s = JSONObject.toJSONString(userDTO);
        redisTemplate.boundValueOps(key).set(s);
        redisTemplate.boundHashOps("login_user").put(key,userDTO);
        redisTemplate.boundValueOps(key).expire(30, TimeUnit.MINUTES);

        return Result.build(ConstantEnum.SUCCESS).setResult(key);
    }

}
